package org.csu.petserver.web;

import jakarta.validation.constraints.NotBlank;
import org.apache.ibatis.annotations.Param;
import org.csu.petserver.domain.LineItem;
import org.csu.petserver.domain.Order;
import org.csu.petserver.service.LineItemInfo;
import org.csu.petserver.service.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@SessionAttributes(value = {"orders"})
public class OrderController {
    @Qualifier("OrderInfo")
    @Autowired
    OrderInfo orderInfo;
    @Autowired
    LineItemInfo lineItemInfo;
    List<Order> orders;
    @GetMapping("/Order/viewAll")
    String viewAll(Model model){
        orders=orderInfo.getAllOrder();
        model.addAttribute("orders",orders);
        Date date=new Date();

        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        String d= sdf.format(date);
        BigDecimal bigDecimal= null;
        try {
            bigDecimal = orderInfo.getTodayBanking(d);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        model.addAttribute("bigDecimal",bigDecimal );
        return "/Order/viewAll";
    }

    @GetMapping("/Order/sortByDate")
    String sortByDate(Model model){
        List<Order> orders1=orderInfo.sortByDate(orders);
        model.addAttribute("orders",orders1);
        return "/Order/viewOrder";
    }

    @GetMapping("/Order/sortByPrice")
    String sortByPrice(Model model){
        List<Order> orders2=orderInfo.sortByPrice(orders);
        model.addAttribute("orders",orders2);
        return "/Order/viewOrder";
    }


    @GetMapping("/Order/viewLineItem")
    String viewLineItem(@Param("orderId") String orderId,Model model){
        List<LineItem> lineItems=orderInfo.getOrderSpecificInfo(orderInfo.selectOrderById(orderId));
        model.addAttribute("lineItems",lineItems);
        return"/Order/viewLineItem";
    }

    @GetMapping("/Order/viewShippingOrder")
    String  viewShippingOrder(Model model){
        List<Order> orders1=orderInfo.selectOrderShipping(orders);
        model.addAttribute("orders",orders1);
        return "/Order/viewOrder";
    }
    @GetMapping("/Order/viewNotShippingOrder")
    String  viewNotShippingOrder(Model model){
        List<Order> orders1=orderInfo.selectOrderNotShipping(orders);
        model.addAttribute("orders",orders1);
        return "/Order/viewOrder";
    }

    @GetMapping("/Order/searchByClient")

    String viewClientOrders(@NotBlank @Param("keyword") String keyword, Model model){
        List<Order> orders1=orderInfo.searchOrderOfAllClient(keyword);
        model.addAttribute("orders",orders1);
        //显示用户的订单总金额
        BigDecimal bigDecimal=new BigDecimal(0);
        for(Order order:orders1){
         bigDecimal=bigDecimal.add(order.getTotalprice());
        }
        model.addAttribute("bigDecimal",bigDecimal);
        return "/Order/viewOrderByClient";
    }

    //接收ajax请求，更改订单状态
    @RequestMapping("/Order/ship")
    void ship(@Param("orderId") String orderId){
        orderInfo.shipping(orderInfo.selectOrderById(orderId));
    }
    //接收ajax请求，更改订单状态
    @RequestMapping("/Order/delete")
    void delete(@Param("orderId") String orderId){
       orderInfo.deleteOrder(orderInfo.selectOrderById(orderId));
    }

}
